// Copyright 2008-2009 BUGCOLLECT.COM
// Author: Remus Rusanu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//                 http://bugcollect.com/terms
//



import com.bugcollect.EResultStatus;
import com.bugcollect.IResponseCallback;
import com.bugcollect.Report;
import com.bugcollect.ReportingManager;
import com.bugcollect.Result;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.*;

/**
 *
 * @author rremus
 */
public class UTReport {

    private static ReportingManager reportingManager;

    public UTReport() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
        reportingManager = new ReportingManager("demoapp", "demo");
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void submitAReport() {
        try
        {
            throw new java.lang.Exception("Test 01");
        }
        catch (Exception e)
        {
            reportingManager.Submit("Noticias", e);
        }
    }

    private class TestResultCallback implements IResponseCallback
    {
        private Report _report;
        public Report getReport()
        {
            return _report;
        }
        private Result _result;
        public Result getResult()
        {
            return _result;
        }
        private Object _state;
        public Object getState()
        {
            return _state;
        }
        private Throwable _exception;
        public Throwable getException()
        {
            return _exception;
        }
        private InputStream _errorStream;
        public InputStream getErrorStream()
        {
            return _errorStream;
        }

        @Override
        public void responseReceived(Report submittedReport, Result receivedResponse, Object state) {
            _report = submittedReport;
            _result = receivedResponse;
            _state = state;
        }

        @Override
        public void submittionException(Report submittedReport, Throwable submittionException,
                InputStream errorStream, Object state) {
            _report = submittedReport;
            _exception = submittionException;
            _errorStream = errorStream;
            _state = state;
        }

    }
    @Test
    public void validateBResult() {
        TestResultCallback callback = new TestResultCallback();
        try
        {
            throw new java.lang.Exception("with callback");
        }
        catch (Exception e)
        {
            reportingManager.Submit(
               new Report("Notice", e),
                    callback,
                    this);
        }

        Result result = callback.getResult();
        if (null != result)
        {
            System.out.println(result);
        }

        InputStream es = callback.getErrorStream();
        if (null != es)
        {
            try
            {
            BufferedReader reader = new BufferedReader(new InputStreamReader(es));
            String line;
            while((line = reader.readLine())!= null)
            {
                System.out.println(line);
            }
            }
            catch(java.io.IOException e)
            {
            
            }
        }

        Report report = callback.getReport();

        Assert.assertNotNull(callback.getReport());
        Assert.assertEquals(callback.getState(), this);
        Throwable callException = callback.getException();
        if (null != callException)
        {
            System.out.println(callException);
        }
        Assert.assertNull(callback.getException());

        Assert.assertEquals(EResultStatus.CREATED, result.getStatus());
        Assert.assertEquals(report.getUUID(), result.getReportUuid());
    }

}